#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
交易 - B2C微支付功能使用示例
"""

import os
import sys

# 添加项目根目录到Python路径，确保能够导入jlpay包
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))

from examples.base_example import BaseExample
from jlpay.services.trans.b2c.micro_pay_service import MicroPayService
from jlpay.model.trans.b2c.micro_pay import (
    MicroPayRequest, MicroPayResponse,
    GoodsDataDto, TerminalInfoDto
)


class MicroPayExample(BaseExample):
    """微支付示例类"""
    
    def __init__(self):
        """初始化示例类"""
        super().__init__("examples.trans.b2c.micro_pay")
        self.micro_pay_service = MicroPayService(self.config)
    
    def build_micro_pay_request(self):
        """构建微支付请求"""
        request = MicroPayRequest()
        
        # 设置请求参数
        request.set_mch_id("849584358120018")
        request.set_term_no("60131234")
        request.set_pay_type("wxpay")
        request.set_out_trade_no("ld201911023465529")
        request.set_payment_valid_time("10")
        request.set_body("充值中心-会员充值")
        request.set_attach("商城—-易拉宝10个打印纸20箱")
        request.set_total_fee("1")
        request.set_auth_code("131208648101095993")
        request.set_limit_pay("no_credit")
        request.set_is_hire_purchase("1")
        request.set_hire_purchase_num("3")
        request.set_hire_purchase_seller_percent("0")
        request.set_remark("该订单物流请走顺丰快递")
        request.set_op_user_id("001")
        request.set_op_shop_id("GD_SZ_001")
        request.set_device_info("N50007A8890")
        request.set_mch_create_ip("123.12.12.123")
        request.set_longitude("116.078631")
        request.set_latitude("39.71274")
        request.set_sub_appid("wxid_129752pfasf1671")
        request.set_goods_tag("sz001")
        
        return request
    
    def demo(self):
        """执行微支付示例"""
        self.logger.info("开始执行微支付示例")
        
        # 创建微支付请求
        request = self.build_micro_pay_request()
        
        # 发送请求
        response = self.send_request_and_handle_response(
            self.micro_pay_service.micro_pay,
            "微支付",
            request
        )
        
        self.logger.info("微支付示例执行完毕")


if __name__ == "__main__":
    logger = MicroPayExample().logger
    logger.info("==================================================")
    logger.info("===== 微支付示例 开始 =====")
    logger.info("==================================================")
    
    example = MicroPayExample()
    example.demo()
    
    logger.info("==================================================")
    logger.info("===== 微支付示例执行完毕 =====")
    logger.info("==================================================") 